<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
             label-position="left"
    >

      <div class="title-container">
        <h3 class="title">vue后台管理系统</h3>
      </div>

      <el-form-item prop="username">
         <span class="svg-container">
            <svg-icon icon-class="user" class="el-input__icon"/>
          </span>


        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        >


        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>

      <!--验证码 -->
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="security"/>
        </span>
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
        <div class="login-code">
          <img :src="img" class="login-code-img" @click="getImageCapcha" title="看不清,点击重新换一张"/>
        </div>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >登录
      </el-button>

    </el-form>
  </div>
</template>

<script>
import { imageCaptcha } from '@/api/login.js'

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度不能小于6位'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        username: 'admin',
        password: 'admin8888',
        code: undefined,
        // 图片 验证码的uuid
        uuid: undefined
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      // 图片验证码base64字符串
      img: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // eleemnt-ui 验证框架是否通过
      this.$refs.loginForm.validate(valid => {
        // 验证全部通过
        if (valid) {
          this.loading = true
          // vuex代码
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            // 更改当前路由地址
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          // 禁止表单提交
          return false
        }
      })
    },
    // 读取图片验证码
    getImageCapcha() {
      imageCaptcha().then(resp => {
        this.img = resp.imgStr
        this.loginForm.uuid = resp.uuid
      })
    }
  },
  created() {
    this.getImageCapcha()
  }
}
</script>
<style>
.login-container .el-input__inner {
  padding-left: 30px;

}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #707070;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url(../../assets/login-background.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;

  .login-form {
    position: relative;
    width: 400px;
    max-width: 100%;
    padding: 25px 25px 5px 25px;
    margin: 0 auto;
    overflow: hidden;
    border-radius: 6px;
    background: #fff;
  }

  .pl30 {
    padding-left: 30px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
    position: absolute;
    top: 0px;
    left: 8px;
    z-index: 1000;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .login-code {
    width: 33%;
    height: 38px;
    float: right;
  }

  .login-code img {
    width: 100%;
  }
}
</style>
